package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PrivateMessage;
import cn.wolfcode.trip.base.mapper.PrivateMessageMapper;
import cn.wolfcode.trip.base.query.PrivateMessageQueryObject;
import cn.wolfcode.trip.base.service.IPrivateMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PrivateMessageServiceImpl implements IPrivateMessageService {
    @Autowired
    private PrivateMessageMapper privateMessageMapper;

    @Override
    public void send(PrivateMessage entity) {
        /*设置发送时间*/
        entity.setSendTime(new Date());
        privateMessageMapper.insert(entity);
    }

    @Override
    public PageInfo queryPrivateMessage(PrivateMessageQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<PrivateMessage> list = privateMessageMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryMessagesByBothId(PrivateMessageQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<PrivateMessage> list = privateMessageMapper.selectByBoth(qo);
        return new PageInfo(list);
    }

    @Override
    public void readMessage(PrivateMessageQueryObject qo) {
        /*不分页*/
        qo.setPageSize(0);
        List<PrivateMessage> list = privateMessageMapper.selectForRead(qo);
        for (PrivateMessage entity : list) {
            /*循环设置已读时间,状态已读*/
            entity.setReadTime(new Date());
            entity.setState(true);
            privateMessageMapper.updateByPrimaryKey(entity);
        }
    }
}
